import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;


public class Player extends Entity
{
	int velocityX = 0;
	int playerSpeed = 2;	
	
	public Player(int x, int y)
	{
		super(x, y);
	}

	public void update()
	{
		x += velocityX;
		checkCollisions();
	}
	
	public void draw(Graphics2D g2)
	{
		g2.drawImage(getPlayerImg(), x, y, null);
		//g2.draw(getBounds());
	}
	
	public Image getPlayerImg()
	{
		ImageIcon ic = new ImageIcon("PlayerShip.png");
		return ic.getImage();
	}
	
	public void keyPressed(KeyEvent e)
	{
		int key = e.getKeyCode();
		
		if (key == KeyEvent.VK_A)
		{
			velocityX = -playerSpeed;
		}
		else if (key == KeyEvent.VK_D)
		{
			velocityX = playerSpeed;
		}
		else if (key == KeyEvent.VK_SPACE)
		{
			Draw.addBullet(new Bullet(x + 15, y));
		}
	}
	
	public void keyReleased(KeyEvent e)
	{
		int key = e.getKeyCode();
		
		if (key == KeyEvent.VK_A)
		{
			velocityX = 0;
		}
		else if (key == KeyEvent.VK_D)
		{
			velocityX = 0;
		}
	}
	
	public void checkCollisions()
	{
		ArrayList<Enemy> enemies = Draw.getEnemyList();
		
		for (int i = 0; i < enemies.size(); i++)
		{
			Enemy tempEnemy = enemies.get(i);
			if (getBounds().intersects(tempEnemy.getBounds()))
			{
				//GameFrame.removeEnemy(tempEnemy);
				System.exit(0);
			}
		}
	}
	
	public Rectangle getBounds()
	{
		return new Rectangle(x, y, getPlayerImg().getWidth(null),getPlayerImg().getHeight(null));
	}
}
